﻿using System;
using System.Reflection;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Ewk.Runtime.Serialization.CharacterSeparated;
using Ewk.Runtime.Serialization.UnitTests.Records.CharacterSeparated;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Ewk.Runtime.Serialization.UnitTests.CharacterSeparated
{
    [TestClass]
    public class CharacterSeparatedDatafieldAttribute_Int32Tests
    {
        [TestMethod, ExpectedException(typeof(DatafieldEmptyException))]
        public void CharacterSeparatedDatafieldAttribute_cannot_deserialize_a_mandatory_empty_Int32_string_with_to_an_Int32_instance()
        {
            var fieldAttribute = new CharacterSeparatedDatafieldAttribute(1) { Mandatory = true };
            Deserialize(fieldAttribute, "");
        }

        [TestMethod]
        public void CharacterSeparatedDatafieldAttribute_can_deserialize_a_mandatory_empty_Int32_string_with_to_an_Int32_instance_when_subscribed_to_the_DatafieldEmpty_event()
        {
            string eventMessage = null;

            var fieldAttribute = new CharacterSeparatedDatafieldAttribute(1) { Mandatory = true };
            fieldAttribute.DatafieldEmpty += delegate(object sender, DatafieldEmptyEventArgs args)
            {
                eventMessage = args.Message;
            };
            var result = Deserialize(fieldAttribute, "");

            Assert.IsInstanceOfType(eventMessage, typeof(string));
            Assert.IsNotNull(result);
        }

        object Deserialize(DatafieldAttribute attribute, string value)
        {
            attribute.PropertyInfo = GetPropertyInfo();
            return attribute.Deserialize(value);
        }

        static PropertyInfo GetPropertyInfo()
        {
            return typeof(CharacterSeparatedTestDatarecordWithIntegerField).GetProperty("Field");
        }

        static PropertyInfo GetPropertyInfo(CharacterSeparatedTestDatarecordWithIntegerField record)
        {
            return record.GetType().GetProperty("Field");
        }
    }
}
